#include "stdafx.h"
#include "../Common/LoggingConsole.h"
#include "../Widgets/DlgAbout.h"
#include "../Widgets/DlgOptions.h"
#include "../Widgets/WdgCoreLogging.h"
#include "../Widgets/WdgCoreOptions.h"
#include "Application.h"
#include "System.h"
#include <QtCore/QFile>

namespace ham
{
	Application::Application(QWidget* parent)
		: Module(parent),
		m_wdgLogging(0), m_logging(0)
	{
		addProperty("Style", QApplication::style()->objectName());
	}
	
	Application::~Application(void)
	{
		delete m_wdgLogging;
		delete m_logging;
	}
	
	QString	Application::getModuleName() const
	{
		return "Core";
	}

	QString Application::getDisplayName() const
	{
		return "LISA Core";
	}

	bool Application::isInit() const
	{
		return m_wdgLogging &&
			Module::isInit();
	}
	
	bool Application::iInit()
	{
		setupUi(this);

		//m_logging = new LoggingConsole();
	
		qDebug() << "Test";
		
		// apply loaded properties
		applyProperties();
		
		// add toolbar toggles to "View" menu
		QList<QMenu*> entries = appMenuBar->findChildren<QMenu*>();
		for (QList<QMenu*>::const_iterator it = entries.begin(); it != entries.end(); it++) {
			QMenu* curMenu = *it;
			if (curMenu->title() == "View") {
				curMenu->addAction(appToolBar->toggleViewAction());
				curMenu->addAction(appLogging->toggleViewAction());
				curMenu->addSeparator();
				break;
			}
		}
		
		// add logging widget to dock widget
		m_wdgLogging = new WdgCoreLogging(this);
		appLoggingLayout->addWidget(m_wdgLogging);

		return true;
	}

	void Application::applyProperties()
	{
		QVector<Module*>& modules = getSystem()->getModules();

		// apply module properties
		for (int i = 0; i < modules.size(); i++) {
			Module* mod = modules[i];
			WdgOptions* optWdg = mod->getOptionsWdg(this);
			optWdg->apply();
			delete optWdg;
		}
	}
	
	WdgOptions* Application::getOptionsWdg(QWidget* parent)
	{
		return new WdgCoreOptions(&m_properties, parent);
	}
	
	void Application::addMenu(QMenu* menu)
	{
		menu->setTearOffEnabled(true);
		
		bool found = false;
		
		// insert before "Help" menu
		QList<QMenu*> entries = appMenuBar->findChildren<QMenu*>();
		for (QList<QMenu*>::const_iterator it = entries.begin(); it != entries.end(); it++) {
			const QMenu* curMenu = *it;
			if (curMenu->title() == "Help") {
				appMenuBar->insertMenu(curMenu->menuAction(), menu);
				found = true;
				break;
			}			
		}

		if (!found)
			appMenuBar->addMenu(menu);
	}

	void Application::addMainWidget(QWidget* widget, QString tabName)
	{
		appMainWidgets->addTab(widget, tabName);
	}

	void Application::addToolbarWidget(QWidget* widget, QString tabName)
	{
		appToolbarWidgets->addTab(widget, tabName);
	}

	void Application::showAboutDlg()
	{
		DlgAbout dlg;
		dlg.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint | Qt::WindowTitleHint | Qt::WindowStaysOnTopHint);
		dlg.exec();	// shows a modal dialog
	}
	
	void Application::showOptionsDlg()
	{
		DlgOptions dlg(getSystem()->getModules());
		dlg.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint | Qt::WindowTitleHint | Qt::WindowStaysOnTopHint);
		dlg.exec();
	}
}